package org.NooLab.itexx.retrieval.google;


/**
 * This implementation class for pre- and postprocessing of google result pages
 * need not to know anything about the "content" of the interfaces (their implementation);
 * 
 * we achieved that by cross-linking interfaces between provider and consumer;
 * such it is a plugin
 * 
 */

import org.NooLab.itexx.retrieval.PageIntf;
import org.NooLab.itexx.retrieval.intf.DocumentsPPPIntf;
import org.NooLab.itexx.retrieval.intf.PPPConsumerIntf;
import org.NooLab.utilities.logging.PrintLog;
import org.apache.commons.lang3.StringUtils;




/**
 * 
 * this class performs operations on the result page of a google query, such that  <br/>
 * - links to cache entries are removed,
 * - top and bottom of page, command bar links are removed,
 * 
 * 
 * 
 */
public class BingPreProc implements DocumentsPPPIntf{
	
	String defaultHTMLOpening = "<!doctype html> <head><title> $$$ </title></head><body>";
	// =================================
	
	// object references ..............
	

	// main variables / properties ....

	
	// constants ......................
	
	
	// volatile variables .............

	
	// helper objects .................
	PrintLog out  ;

	
	public BingPreProc( PPPConsumerIntf consumer ){
		
		// this should send an anchor to the main class , such that this preproc routine
		// can be executed over there
		
		consumer.addPreProcessor( this ) ;
		
	}
	/*
	      
	
	*/
	 
	public void performPPP( PageIntf page) {
		String str, pageAddress, htmlContent = "" ;
		
		try{
			
			pageAddress = page.getWebURL().getURL() ;

			// just for DEBUG page.setText( (String) ("<!--  abc -->" + page.getText()) ) ;
			
			if (( pageAddress.toLowerCase().contains("www.google.")==false) || 
				(page.getContentType().contains("text")==false )){
				return ;
			}
			
			
			str = page.getHTML() ;
			
			// do the adjustments here
			// we remove the stuff we do not need : headers, finals, href attributes
			// then re-assembling a valid HTML doc again, which can be parsed
			
			str = StringUtils.substringBetween( htmlContent, "<title>","</title>") ;
			
			// put the changes back to the page :: object
			
			System.out.println(" google preproc applied...");
			page.setHTML(str) ;
			
			
		}catch(Exception e){
			e.printStackTrace() ;
		}	
		
		
	}
	
	public String performPPP(String htmltext) {
		
		String str="";
		
		
		
		try{
			
			str = htmltext ;
			
		}catch(Exception e){
			
		}
		return str ;
	}
	
	
	
}
